package com.mfy.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.mfy.entity.Video;
import com.mfy.service.IVideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 视频 前端控制器
 * </p>
 *
 * @author mfy
 * @since 2023-05-09
 */
@RestController
@RequestMapping
@Slf4j
public class VideoController {
    @Autowired
    private IVideoService videoService;
    @GetMapping("/videos")
    public Map<String, Object> videos(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "per_page", defaultValue = "5") Integer rows,
                                      String id,    //视频id
                                      String name,  //视频名称
                                      @RequestParam(value = "category_id", required = false) String categoryId,  //类别id
                                      @RequestParam(value = "uploader_name", required = false) String username   //根据up主 根据用户名查
    ) {
        Map<String, Object> result = new HashMap<>();
        log.info("当前页为: {}", page);
        log.info("每页显示记录数: {}", rows);
        log.info("搜索条件id是否存在:{}, id为: {}", !ObjectUtils.isEmpty(id), id);
        log.info("搜索条件name是否存在:{}, name为: {}", !ObjectUtils.isEmpty(name), name);
        log.info("搜索条件category_id是否存在:{}, category_id为: {}", !ObjectUtils.isEmpty(categoryId), categoryId);
        log.info("搜索条件uploader_name是否存在:{}, uploader_name为: {}", !ObjectUtils.isEmpty(username), username);
        //根据条件搜索的服务条件的记录
        Page<Video> pageInfo = new Page<>(page,rows);
        IPage<Video> allByKeywords = videoService.findAllByKeywords(id, name, categoryId, username, pageInfo);
        log.info("符合条件的总数: {}", allByKeywords.getTotal());
        result.put("total_count", allByKeywords.getTotal());
        result.put("items", allByKeywords.getRecords());
        return result;
    }
}

